package top.smallway.bs_vr.recycelviewAdapeter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import top.smallway.bs_vr.R;
import top.smallway.bs_vr.requestsBS.Song;
import top.smallway.bs_vr.second_song;

public class SongAdapeter extends RecyclerView.Adapter<SongAdapeter.MyViewHolder> {
    public SongAdapeter(List<Song> data, Context context) {
        this.data = data;
        this.context = context;
    }

    private List<Song> data;
    private Context context;
    private static final int VIEW_TYPE_NORMAL = 0;
    private static final int VIEW_TYPE_FOOTER = 1;
    private boolean mIsLoadMoreEnabled = false;


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_footer, parent, false);
            return new FooterViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
            return new MyViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (getItemViewType(position) == VIEW_TYPE_NORMAL) {
            Song song = data.get(position);
            holder.song_name.setText(song.getSongName());
            holder.song_speed.setText("速度："+song.getDifficulty());
            holder.author_name.setText(song.getSongAuthorName());
            Glide.with(holder.itemView.getContext()).load(song.getSongPicURL())
                    .placeholder(R.drawable.moxi)
                    .error(R.drawable.moxi)
                    .into(holder.pic);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), second_song.class);
                    intent.putExtra("song_name",data.get(position).getSongName());
                    intent.putExtra("song_speed",data.get(position).getDifficulty());
                    intent.putExtra("author_name",data.get(position).getSongAuthorName());
                    intent.putExtra("SongPicURL",data.get(position).getSongPicURL());
                    intent.putExtra("downloadURL",data.get(position).getDownloadURL());
                    v.getContext().startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mIsLoadMoreEnabled ? (data == null ? 1 : data.size() + 1) : (data == null ? 0 : data.size());
    }

    @Override
    public int getItemViewType(int position) {
        if (mIsLoadMoreEnabled && position >= getItemCount() - 1) {
            return VIEW_TYPE_FOOTER;
        }
        return VIEW_TYPE_NORMAL;
    }
    public void setLoadMoreEnabled(boolean enabled) {
        mIsLoadMoreEnabled = enabled;
        notifyDataSetChanged();
    }
    public void updateData(List<Song> newData) {
        data.addAll(newData);
        notifyDataSetChanged();
    }
    public void clearData(){
        data.clear();
        notifyDataSetChanged();
    }
    private static class FooterViewHolder extends MyViewHolder {
        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView pic;
        private TextView song_name, author_name, song_speed;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            pic = itemView.findViewById(R.id.pic);
            song_name = itemView.findViewById(R.id.song_name);
            author_name = itemView.findViewById(R.id.author_name);
            song_speed = itemView.findViewById(R.id.song_speed);
        }
    }
}
